<script lang="ts" setup>
import type { CardParams } from "@/types/card";
import type { FormRules } from "element-plus";
import { reactive } from "vue";

// 1. 添加月卡
// 车辆信息表单，Partial 泛型工具类转可选
const carInfoForm = reactive<Partial<CardParams>>({
  personName: "", // 车主姓名
  phoneNumber: "", // 联系方式
  carNumber: "", // 车牌号码
  carBrand: "", // 车辆品牌
});

// 表单验证，非空和正则校验
const carInfoFormRules = reactive<FormRules<typeof carInfoForm>>({
  personName: [
    { required: true, message: "请输入车主姓名", trigger: "blur" },
    {
      pattern: /^(?:[\u4e00-\u9fa5·]{2,16})$/,
      message: "长度在 2 到 10 个字符",
      trigger: "blur",
    },
  ],
  phoneNumber: [
    { required: true, message: "请输入联系方式", trigger: "blur" },
    {
      pattern:
        /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
      message: "手机号格式不正确",
      trigger: "blur",
    },
  ],
  carNumber: [
    { required: true, message: "请输入车牌号码", trigger: "blur" },
    {
      pattern:
        /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/,
      message: "长度为 7 位",
      trigger: "blur",
    },
  ],
  carBrand: [{ required: true, message: "请输入车辆品牌", trigger: "blur" }],
});
</script>

<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header content="增加月卡" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form
            :model="carInfoForm"
            :rules="carInfoFormRules"
            label-width="100px"
          >
            <el-form-item label="车主姓名" prop="personName">
              <el-input
                v-model="carInfoForm.personName"
                clearable
                autocomplete="on"
              />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input :maxlength="11" v-model="carInfoForm.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-memo="carInfoForm.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="carInfoForm.carBrand" />
            </el-form-item>
          </el-form>
          {{ carInfoForm }}
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form label-width="100px">
            <el-form-item label="有效日期">
              <el-date-picker
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额">
              <el-input />
            </el-form-item>
            <el-form-item label="支付方式">
              <el-select>
                <!-- <el-option v-for="item in [{}]" :key="item.id" :value="item.id" :label="item.name" /> -->
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
